import math

def adjust_speeds_preserve_mean(v1, v2, δ_R, d):
    """速度调整函数（保持平均值不变）"""
    # 计算当前平均值和半径
    v_mean = (v1 + v2) / 2
    if abs(v2 - v1) < 1e-6:
        R_curr = float('inf')
    else:
        R_curr = d * (v1 + v2) / (v2 - v1)
    
    # 计算新半径（带边界约束）
    if R_curr == float('inf'):
        R_new = δ_R
    else:
        R_new = R_curr + δ_R
        R_max = 100 * abs(d)
        if abs(R_new) > R_max:
            R_new = R_max if R_new > 0 else -R_max
    
    # 处理特殊情形
    if abs(R_new) > 1e6:  # 直行
        return (v_mean, v_mean)
    elif abs(R_new) < 1e-6:  # 原地旋转
        return (0.0, 2*v_mean) if δ_R > 0 else (2*v_mean, 0.0)
    
    # 常规计算
    k = (2 * d) / R_new
    k = max(min(k, 2.0), -2.0)  # 物理限制
    
    v1_new = v_mean * (1 - k / 2)
    v2_new = v_mean * (1 + k / 2)
    return (max(v1_new, 0.0), max(v2_new, 0.0))

def run_test_case(v1, v2, δ_R, d, case_name):
    """执行单个测试案例（增强版）"""
    print(f"\n【{case_name}】")
    print(f"初始速度: v1={v1:.2f}, v2={v2:.2f}")
    print(f"调整量 δ_R: {δ_R:.2f}")
    
    # 计算当前状态
    v_mean = (v1 + v2) / 2
    R_curr = float('inf') if abs(v2-v1)<1e-6 else d*(v1+v2)/(v2-v1)
    
    # 执行调整
    v1_new, v2_new = adjust_speeds_preserve_mean(v1, v2, δ_R, d)
    
    # 计算新状态
    v_mean_new = (v1_new + v2_new) / 2
    R_new = float('inf') if abs(v2_new-v1_new)<1e-6 else d*(v1_new+v2_new)/(v2_new-v1_new)
    delta_actual = R_new - R_curr if (R_curr!=float('inf') and R_new!=float('inf')) else float('nan')
    
    # 验证条件
    radius_pass = (abs(delta_actual - δ_R) < 1e-6 if not math.isnan(delta_actual) 
                  else (R_new == δ_R if R_curr == float('inf') else True))
    mean_pass = abs(v_mean_new - v_mean) < 1e-6
    
    # 输出结果（增强版）
    print(f"当前半径: {R_curr:.3f} → 新半径: {R_new:.3f}")
    print(f"速度变化: ({v1:.2f}, {v2:.2f}) → ({v1_new:.2f}, {v2_new:.2f})")
    print(f"平均速度: {v_mean:.3f} → {v_mean_new:.3f}")
    print(f"半径变化: {delta_actual:.3f} (期望: {δ_R:.3f})")
    print(f"测试结果: Radius={'PASS' if radius_pass else 'FAIL'}, Mean={'PASS' if mean_pass else 'FAIL'}")

# 系统参数
d = 0.5  # 轮间距的一半

# ===== 测试案例组 =====

# 1. 直行 → 转弯 (4个例子)
print("\n" + "="*60 + "\n1. 直行 → 转弯测试\n" + "="*60)
run_test_case(1.0, 1.0, 1.0, d, "直行 → 左转 (R=1.0)")
run_test_case(0.8, 0.8, -0.5, d, "直行 → 右转 (R=-0.5)") 
run_test_case(1.5, 1.5, 0.3, d, "直行 → 左转 (R=0.3)")
run_test_case(2.0, 2.0, -1.2, d, "直行 → 右转 (R=-1.2)")

# 2. 转弯 → 直行 (4个例子)
print("\n" + "="*60 + "\n2. 转弯 → 直行测试\n" + "="*60)
run_test_case(1.2, 0.8, 2.0, d, "右转 → 直行 (δ=2.0)")
run_test_case(0.7, 1.3, -1.5, d, "左转 → 直行 (δ=-1.5)")
run_test_case(1.1, 0.9, 1.8, d, "右转 → 直行 (δ=1.8)")
run_test_case(0.6, 1.4, -2.0, d, "左转 → 直行 (δ=-2.0)")

# 3. 直行 → 直行 (4个例子)
print("\n" + "="*60 + "\n3. 直行 → 直行测试\n" + "="*60)
run_test_case(1.5, 1.5, float('inf'), d, "直行 → 保持直行 (δ=∞)")
run_test_case(0.6, 0.6, 0.0, d, "直行 → 保持直行 (δ=0)")
run_test_case(2.0, 2.0, float('inf'), d, "高速直行 → 保持")
run_test_case(0.3, 0.3, 0.0, d, "低速直行 → 保持")

# 4. 转向变急 (4个例子)
print("\n" + "="*60 + "\n4. 转向变急测试\n" + "="*60)
run_test_case(1.1, 0.9, -0.3, d, "右转增强 (δ=-0.3)")
run_test_case(0.6, 1.4, 0.4, d, "左转增强 (δ=0.4)")
run_test_case(1.3, 0.7, -0.5, d, "右转增强 (δ=-0.5)")
run_test_case(0.5, 1.5, 0.2, d, "左转增强 (δ=0.2)")

# 5. 转向变缓 (4个例子)
print("\n" + "="*60 + "\n5. 转向变缓测试\n" + "="*60)
run_test_case(1.3, 0.7, 0.5, d, "右转减弱 (δ=0.5)")
run_test_case(0.5, 1.0, -0.2, d, "左转减弱 (δ=-0.2)")
run_test_case(1.4, 0.6, 0.8, d, "右转减弱 (δ=0.8)")
run_test_case(0.4, 1.1, -0.3, d, "左转减弱 (δ=-0.3)")